package cn.it62.pojo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zhenhuaShen
 * @version 1.0
 * @date 2019/8/7
 * @description cn.it62.pojo
 */
public class ListDemo {
    public static void main(String[] args) {
        //1、创建一个集合对象
        List list = new ArrayList();

        //添加元素
        list.add(1);
        list.add("");
        list.add(null);
        list.add(1 > 2);
        list.add(null);
        list.add(new Person("it62_001", 22, "tom"));
        list.add(new Person("it62_001", 22, "tom"));

        //获得集合长度，元素个数
        System.out.println(list.size());

        //判断集合是否是空的
        System.out.println(list.isEmpty());

        //判断集合是否包含某个值
        System.out.println(list.contains(1 > 2));
        System.out.println(list.contains(new Person("it62_001", 22, "tom")));

        //元素首次出现的位置
        int index = list.indexOf(new Person("it62_001", 22, "tom"));
        //5
        System.out.println(index);

        //元素最后一次出现的位置
        int index1 = list.lastIndexOf(null);
        System.out.println(index1);

        //把集合转数组
        Object[] objects = list.toArray();
        for (Object o : objects) {
            System.out.println(o);
        }

        System.out.println("--------------------------------------------");
        //获得指定位置的元素
        Object o = list.get(0);
        System.out.println(o);
        for (int i = 0, len = list.size(); i < len; i++) {
            Object object = list.get(i);
            System.out.println(object);
        }

        System.out.println("--------------------------------------------");
        //set方法 在指定的位置放一个对象，相当于update,返回旧的元素
        Object object = list.set(4, "hello");
        System.out.println(object);
        System.out.println(list);

        System.out.println("--------------------------------------------");
        //在指定的位置插入一个元素
        list.add(4, "world");
        System.out.println(list);
        System.out.println("--------------------------------------------");
        //单个删除指定位置的元素
        int index2 = list.indexOf("wrld");
        if (index2 >= 0) {
            System.out.println("删除" + list.remove(index2) + "成功");
        } else {
            System.out.println("没有要删除的元素！");
        }
        System.out.println(list);

        System.out.println("--------------------------------------------");
        //单个删除指定首次出现的元素
        System.out.println(list.remove(new Person("it62_001", 22, "tom")));
        System.out.println(list);

        System.out.println("--------------------------------------------");
        //清空所有元素
        //list.clear();
        //System.out.println(list);

        System.out.println("--------------------------------------------");
        //任意一个集合底层都有一个迭代器属性
        Iterator iterator = list.iterator();
        System.out.println(iterator.getClass().getName());
        while (iterator.hasNext()){
            Object object1=iterator.next();
            if (object1 instanceof Person){
                System.out.println(((Person) object1).getUseName());
            }
        }
    }
}